package RoleController;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.renjun.smweb.commons.util.Encrypt;
import com.renjun.smweb.dao.pojo.LoginInfo;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.renjun.smweb.controller.RoleController.RoleController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by huamin on 2017/2/23.
 * 角色管理M2
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath*:/spring-mvc.xml", "classpath*:/spring-mybatis.xml"})
public class RoleControllerTestCase {

    @Autowired
    private RoleController roleController;

    //模拟request,response
    private MockHttpServletRequest request;
    private MockHttpServletResponse response;

    @Before
    public void setUp() throws Exception {
        request = new MockHttpServletRequest();
        request.getSession().setAttribute("userinfo", getLoginfo());
//        response = new MockHttpServletResponse();
    }

    //获取缓存LoginInfo
    private LoginInfo getLoginfo() {
        LoginInfo loginInfo = new LoginInfo();
        loginInfo.setId("999");
        loginInfo.setRoleid("0");
        loginInfo.setDistrictcode("310116");
        loginInfo.setRoletype("2");
        loginInfo.setUsername("测试123");
        loginInfo.setRealname("junit测试管理员");
        return loginInfo;
    }

    /**
     * 角色列表M2-1
     *
     * @param {limit:10,offset:0,order:"asc",sort:"rolename"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testQueryRoleList(){
        System.out.println("M2-1查询角色列表");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("limit","10");
        params.put("offset","0");
        params.put("order","");
        params.put("sort","");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //角色列表M2-1
        JSONObject rs = roleController.queryRoleList(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" +Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 新增一个角色M2-2
     *
     * @param {rolename:"",roletype:"",roledesc:""}
     * @return
     * by huamin
     */
    @Test
    public void testAddNewRole(){
        System.out.println("新增一个角色M2-2");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("rolename","测试员名称");
        params.put("roletype","0");
        params.put("roledesc","测试员描述");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //新增一个角色M2-2
        JSONObject rs = roleController.addNewRole(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" +Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 修改角色信息M2-3
     *
     * @param {roleid:"",rolename:"",roletype:"",roledesc:""}
     * @return
     * by huamin
     */
    @Test
    public void testUpdateRole(){
        System.out.println("修改角色信息M2-3");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("roleid","999");
        params.put("rolename","测试员名称2");
        params.put("roletype","1");
        params.put("roledesc","测试员描述2");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //修改角色信息M2-3
        JSONObject rs = roleController.updateRole(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" +Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * M2-4恢复或注销角色信息
     *
     * @param {roleid:"",enabled:""(1恢复0注销)}
     * @return
     * by huamin
     */
    @Test
    public void testRecoverOrCancelRole(){
        System.out.println("M2-4恢复或注销角色信息");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("roleid","999");
        params.put("enabled","1");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M2-4恢复或注销角色信息
        JSONObject rs = roleController.recoverOrCancelRole(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" +Encrypt.jiem(jiem)+ "\n");
    }

    /**
     * 角色查看M2-5
     *
     * @param {roleid:"2"}
     * @return [{"id":"2","rolename":"市管理员","roletype":"1","roledesc":"市管理员","enabled":"1"}]
     * by huamin
     */
    @Test
    public void testQueryRoleById(){
        System.out.println("M2-5角色查看");
        //封装参数
        Map<String,Object> params=new HashMap<>();
        params.put("roleid","999");
        params.put("enabled","1");
        //map转json字符串
        request.setContent(JSONObject.toJSONString(params).getBytes());
        //M2-4恢复或注销角色信息
        JSONObject rs = roleController.queryRoleById(request, response);
        String jiem = (String) rs.get("response");
        System.out.println("\n json对象：" +Encrypt.jiem(jiem)+ "\n");
    }

}
